﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
    public float hp;    //血量
    public float maxHp = 100f;  //满血状态
    public Slider hpBar;    //血条组件
    public GameObject dieObj;

    private void Awake()
    {
        hp = maxHp;
        if (hpBar == null)
            hpBar = transform.Find("HpBar/Slider").GetComponent<Slider>();
        hpBar.maxValue = maxHp; //血条最大范围值
        hpBar.value = hp;       //当前血量
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(hp<=0)
        {
            Destroy(gameObject);
            Instantiate(dieObj, transform);

        }
    }

    //减血
    public void TakeDamage(float damage)
    {
        if(hp>damage)
        {
            hp -= damage;
        }
        else
        {
            hp = 0;
            //坦克爆炸销毁
        }
    }
}
